home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / bit / src / ulib / readint.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  3KB  |  167 lines

  1. /***********************************************************************
  2.  * $Id: readint.c,v 0.80 1994/02/24 09:48:11 zhao Exp $
  3.  *
  4.  *.  Copyright(c) 1993,1994 by T.C. Zhao
  5.  *   All rights reserved.
  6.  *.
  7.  *
  8.  *   Read an integer (decimal or hex) from a file.
  9.  *   Valid sepertors are  SPACE, \t \n  and ,
  10.  *   Comment is introduced by #
  11.  *
  12.  * For hex integers, a-f, A-F is assumed continous
  13.  ***********************************************************************/
  14. #if !defined(lint) && defined(F_ID)
  15. char *id_rdint = "$Id: readint.c,v 0.80 1994/02/24 09:48:11 zhao Exp $";
  16. #endif
  17.  
  18. #include <stdio.h>
  19. #include <ctype.h>        /* for isdigit */
  20. #include "ulib.h"
  21.  
  22. #define IS_FS(c)      ((c)==' ' || (c)=='\t' || (c)=='\n' || (c)==',' )
  23. #define IS_COMMENT(c) ((c) == '#')
  24.  
  25. static int yell;
  26.  
  27. void
  28. set_read_silent(int y)
  29. {
  30.     yell = !y;
  31. }
  32.  
  33. void
  34. bad_character(int c)
  35. {
  36.     if (yell && c != EOF)
  37.     (void) fprintf(stderr, "Bad character %c Code=%d\n", c, c);
  38. }
  39.  
  40. int
  41. skip_comment(FILE * fp)
  42. {
  43.     register int c;
  44.     while ((c = getc(fp)) != EOF && c != '\n')
  45.     ;
  46.     return (c != EOF) ? getc(fp) : EOF;
  47. }
  48.  
  49. /* read an integer [+-]nnn. No way to return error status */
  50. int
  51. readint(FILE * fp)
  52. {
  53.     register int c, num = 0, sign = 1;
  54.  
  55.     do
  56.       {
  57.       c = getc(fp);
  58.       while (IS_COMMENT(c))
  59.           c = skip_comment(fp);
  60.       }
  61.     while (IS_FS(c));
  62.  
  63.     if (c == '-' || c == '+')
  64.       {
  65.       sign = (c == '-') ? -1 : 1;
  66.       c = getc(fp);
  67.       }
  68.  
  69.     while (isdigit(c))
  70.       {
  71.       num = 10 * num + c - '0';
  72.       c = getc(fp);
  73.       }
  74.     if (!IS_FS(c))
  75.       {
  76.       bad_character(c);
  77.       num = 123456;
  78.       }
  79.     return sign * num;
  80. }
  81.  
  82. /* Read a positive integer. return EOF if error */
  83. int
  84. readpint(FILE * fp)
  85. {
  86.     register int c, num = 0;
  87.  
  88.     do
  89.       {
  90.       c = getc(fp);
  91.       while (IS_COMMENT(c))
  92.           c = skip_comment(fp);
  93.       }
  94.     while (IS_FS(c));
  95.  
  96.     if (!(c == '+' || isdigit(c)))
  97.       {
  98.       bad_character(c);
  99.       return EOF;
  100.       }
  101.     do
  102.       {
  103.       num = 10 * num + c - '0';
  104.       c = getc(fp);
  105.       }
  106.     while (isdigit(c));
  107.     return num;
  108. }
  109.  
  110. /*
  111.  * Read a hex integer
  112.  */
  113. int
  114. readhexint(FILE * fp)
  115. {
  116.     register int num = 0, i, c;
  117.     static short hextab[256];
  118.  
  119. /* initialize the hex table */
  120.     if (!hextab['1'])
  121.       {
  122.       for (i = '1'; i <= '9'; i++)
  123.           hextab[i] = i - '0';
  124.       for (i = 'A'; i <= 'F'; i++)
  125.           hextab[i] = 10 + i - 'A';
  126.       for (i = 'a'; i <= 'f'; i++)
  127.           hextab[i] = 10 + i - 'a';
  128.       }
  129.     do
  130.       {
  131.       c = getc(fp);
  132.       while (IS_COMMENT(c))
  133.           c = skip_comment(fp);
  134.       }
  135.     while (IS_FS(c));
  136.  
  137. /*
  138.  * demand  0[xX]
  139.  */
  140.     if (c != '0' || ((c = getc(fp)) != 'x' && c != 'X'))
  141.       {
  142.       bad_character(c);
  143.       return EOF;
  144.       }
  145. /*
  146.  * now do the coversion
  147.  */
  148.     while ((c = getc(fp)), isxdigit(c))
  149.     num = (num << 4) + hextab[c];
  150.     return num;
  151. }
  152.  
  153. #ifdef TEST
  154. #include <stdio.h>
  155. #include <string.h>
  156.  
  157. main()
  158. {
  159.     int p;
  160.  
  161.     while ((p = readint(stdin)) != -1)
  162.     fprintf(stderr, "Read: %d\n", p);
  163.  
  164. }
  165.  
  166. #endif
  167.